package com.hc.fdkforwarders.dao;

import com.hc.fdkforwarders.db.tables.records.BaseBusinessInfoRecord;
import com.hc.fdkforwarders.vo.AllCityMarketFloorVO;
import com.hc.fdkforwarders.vo.CityPassVO;
import com.hc.fdkforwarders.vo.FloorListVO;
import com.hc.fdkforwarders.vo.MarketListVO;
import org.jooq.Record1;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static com.hc.fdkforwarders.db.tables.BaseBusinessInfo.BASE_BUSINESS_INFO;

@Repository
public class BaseBusinessInfoDao extends BaseDao{
    //代拿 接口1
    public List<CityPassVO> cityPassVOList () {
        List<CityPassVO> cityPassVOList = new ArrayList<CityPassVO>();
        List<MarketListVO> marketListVOList=new ArrayList<MarketListVO>();
        List<FloorListVO> floorListVOList=new ArrayList<FloorListVO>();
        MarketListVO marketListVO = new MarketListVO();
        FloorListVO floorListVO = new FloorListVO();
        CityPassVO cityPassVO =  new CityPassVO();

        List<AllCityMarketFloorVO> allCityMarketFloorVOS = db.selectDistinct(BASE_BUSINESS_INFO.CITY_CODE,
                BASE_BUSINESS_INFO.CITY_NAME,
                BASE_BUSINESS_INFO.FLOOR_CODE,
                BASE_BUSINESS_INFO.FLOOR_NAME,
                BASE_BUSINESS_INFO.MARKET_CODE,
                BASE_BUSINESS_INFO.MARKET_NAME)
                .from(BASE_BUSINESS_INFO)
                .fetchInto(AllCityMarketFloorVO.class);

        for (AllCityMarketFloorVO allCityMarketFloorVO : allCityMarketFloorVOS) {
            cityPassVO.setCityName(allCityMarketFloorVO.getCityName());
            cityPassVO.setCityCode(allCityMarketFloorVO.getCityCode());
            marketListVOList.clear();
            for (AllCityMarketFloorVO cityMarketFloorV1 : allCityMarketFloorVOS) {
                if(cityMarketFloorV1.getCityCode().equals(allCityMarketFloorVO.getCityCode())){
                    marketListVO.setMarketName(cityMarketFloorV1.getMarketName());
                    marketListVO.setMarketCode(cityMarketFloorV1.getMarketCode());
                    marketListVO.setCityCode(cityMarketFloorV1.getCityCode());
                    floorListVOList.clear();
                    for (AllCityMarketFloorVO allCityMarketFloorV2 : allCityMarketFloorVOS) {
                        if(allCityMarketFloorV2.getMarketCode().equals(cityMarketFloorV1.getMarketCode())){
                            floorListVO.setFloorCode(allCityMarketFloorVO.getFloorCode());
                            floorListVO.setFloorName(allCityMarketFloorVO.getFloorName());
                            floorListVO.setMarketCode(allCityMarketFloorVO.getMarketCode());
                            floorListVOList.add(floorListVO);//list
                        }
                    }
                    marketListVO.setFloorList(floorListVOList);
                    marketListVOList.add(marketListVO);
                }
            }
            cityPassVO.setMarketList(marketListVOList);
            cityPassVOList.add(cityPassVO);
        }
        return cityPassVOList;
    }
}
